package com.liaofan.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

public class JedisTest {
    public static void main(String[] args) {
        //创建链接
        Jedis jedis = new Jedis("192.168.153.129", 6379);

        String value = jedis.ping();
        jedis.set("k1", "v1 v2 v3 v4");
        jedis.set("k2", "v4 v5 v6 v7");

        System.out.println(value);

    }

    @Test
    public void caoZuoKey() {
        Jedis jedis = new Jedis("192.168.153.129", 6379);
        Set<String> set = jedis.keys("*");

        jedis.set("name", "luck");
        jedis.mset("n1", "v1", "n2", "v2");
        System.out.println(jedis.mget("n1", "n2"));


        for (String key : set) {
            System.out.println(key);

            System.out.println(jedis.get(key));
            System.out.println(jedis.ttl(key));
        }

    }

    @Test
    public void modifyList() {
        Jedis jedis = new Jedis("192.168.153.129", 6379);
        jedis.rpush("keyMethod", "luck", "mary", "marryMack");
        List<String> jedisList = jedis.lrange("keyMethod", 0, -1);

        for (String key : jedisList) {
            System.out.println("list中的元素：" + key);
        }
    }

    /**
     * 操作set
     */
    @Test
    public void modifySet() {
        Jedis jedis = new Jedis("192.168.153.129", 6379);
        jedis.select(0);
        jedis.sadd("name_set", "jack", "luck");
        Set<String> smembers_set = jedis.smembers("name_set");

        for (String key : smembers_set) {
            System.out.println("set中的元素：" + key);
        }
    }


    /**
     * 操作Zset
     */
    @Test
    public void modifyZset() {
        Jedis jedis = new Jedis("192.168.153.129", 6379);
        jedis.select(0);
        jedis.zadd("city", 100, "上海");
        jedis.zadd("city", 90, "北京");
        jedis.zadd("city", 0, "成都");
        Set<String> zrangSet = jedis.zrangeByScore("city", 0, 100);

        for(String key : zrangSet) {
            System.out.println(key);
        }

    }

    /**
     * 清除redis数据库
     */
    @Test
    public void flushdb() {
        Jedis jedis = new Jedis("192.168.153.129", 6379);
        jedis.flushDB();
        System.out.println("清除redis数据库成功");
    }
}
